//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SwapRateHelper.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Instruments/VanillaSwap.h>
#include <gen/QL/Termstructures/Yield/RelativeDateRateHelper.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (Double rate, Cephei::QL::Indexes::ISwapIndex^ swapIndex, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>^ spread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ fwdStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountingCurve) : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
    CSwapIndex^ _CswapIndex;
    CQuote^ _Cspread;
    CPeriod^ _CfwdStart;
    CYieldTermStructure^ _CdiscountingCurve;
    try
    {
#ifdef HANDLE
        _phSwapRateHelper = NULL;
#endif
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate);
        _CswapIndex = safe_cast<CSwapIndex^> (swapIndex);
        _CswapIndex->Lock();
        boost::shared_ptr<QuantLib::SwapIndex>& _swapIndex = static_cast<boost::shared_ptr<QuantLib::SwapIndex>&> (_CswapIndex->GetShared ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>::IsSome::get (spread))
        {
            _Cspread = safe_cast<CQuote^> (spread->Value);
            _Cspread->Lock();
        }
        Handle<QuantLib::Quote>& _spread = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>::IsSome::get (spread) ? static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()) : Handle<QuantLib::Quote>()); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (fwdStart))
        {
            _CfwdStart = safe_cast<CPeriod^> (fwdStart->Value);
            _CfwdStart->Lock();
        }
        QuantLib::Period& _fwdStart = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (fwdStart) ? static_cast<QuantLib::Period&> (_CfwdStart->GetReference ()) : 0*Days); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountingCurve))
        {
            _CdiscountingCurve = safe_cast<CYieldTermStructure^> (discountingCurve->Value);
            _CdiscountingCurve->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _discountingCurve = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountingCurve) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountingCurve->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppSwapRateHelper = new boost::shared_ptr<QuantLib::SwapRateHelper> (new QuantLib::SwapRateHelper ( _rate,  _swapIndex,  _spread,  _fwdStart,  _discountingCurve ));
        SetRelativeDateRateHelper (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppSwapRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CswapIndex != nullptr) _CswapIndex->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
        if (_CfwdStart != nullptr) _CfwdStart->Unlock();
        if (_CdiscountingCurve != nullptr) _CdiscountingCurve->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (Double rate, Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum fixedFrequency, QL::Times::BusinessDayConventionEnum fixedConvention, Cephei::QL::Times::IDayCounter^ fixedDayCount, Cephei::QL::Indexes::IIborIndex^ iborIndex, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>^ spread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ fwdStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountingCurve) : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
    CPeriod^ _Ctenor;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CfixedDayCount;
    CIborIndex^ _CiborIndex;
    CQuote^ _Cspread;
    CPeriod^ _CfwdStart;
    CYieldTermStructure^ _CdiscountingCurve;
    try
    {
#ifdef HANDLE
        _phSwapRateHelper = NULL;
#endif
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate);
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Frequency _fixedFrequency = (QuantLib::Frequency)fixedFrequency ;
        QuantLib::BusinessDayConvention _fixedConvention = (QuantLib::BusinessDayConvention)fixedConvention ;
        _CfixedDayCount = safe_cast<CDayCounter^> (fixedDayCount);
        _CfixedDayCount->Lock();
        QuantLib::DayCounter& _fixedDayCount = static_cast<QuantLib::DayCounter&> (_CfixedDayCount->GetReference ()); 
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>::IsSome::get (spread))
        {
            _Cspread = safe_cast<CQuote^> (spread->Value);
            _Cspread->Lock();
        }
        Handle<QuantLib::Quote>& _spread = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>::IsSome::get (spread) ? static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()) : Handle<QuantLib::Quote>()); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (fwdStart))
        {
            _CfwdStart = safe_cast<CPeriod^> (fwdStart->Value);
            _CfwdStart->Lock();
        }
        QuantLib::Period& _fwdStart = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (fwdStart) ? static_cast<QuantLib::Period&> (_CfwdStart->GetReference ()) : 0*Days); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountingCurve))
        {
            _CdiscountingCurve = safe_cast<CYieldTermStructure^> (discountingCurve->Value);
            _CdiscountingCurve->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _discountingCurve = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountingCurve) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountingCurve->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppSwapRateHelper = new boost::shared_ptr<QuantLib::SwapRateHelper> (new QuantLib::SwapRateHelper ( _rate,  _tenor,  _calendar,  _fixedFrequency,  _fixedConvention,  _fixedDayCount,  _iborIndex,  _spread,  _fwdStart,  _discountingCurve ));
        SetRelativeDateRateHelper (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppSwapRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CfixedDayCount != nullptr) _CfixedDayCount->Unlock();
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
        if (_CfwdStart != nullptr) _CfwdStart->Unlock();
        if (_CdiscountingCurve != nullptr) _CdiscountingCurve->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (Cephei::QL::IQuote^ rate, Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum fixedFrequency, QL::Times::BusinessDayConventionEnum fixedConvention, Cephei::QL::Times::IDayCounter^ fixedDayCount, Cephei::QL::Indexes::IIborIndex^ iborIndex, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>^ spread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ fwdStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountingCurve) : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
    CQuote^ _Crate;
    CPeriod^ _Ctenor;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CfixedDayCount;
    CIborIndex^ _CiborIndex;
    CQuote^ _Cspread;
    CPeriod^ _CfwdStart;
    CYieldTermStructure^ _CdiscountingCurve;
    try
    {
#ifdef HANDLE
        _phSwapRateHelper = NULL;
#endif
        _Crate = safe_cast<CQuote^> (rate);
        _Crate->Lock();
        Handle<QuantLib::Quote>& _rate = static_cast<Handle<QuantLib::Quote>&> (_Crate->GetHandle ()); 
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Frequency _fixedFrequency = (QuantLib::Frequency)fixedFrequency ;
        QuantLib::BusinessDayConvention _fixedConvention = (QuantLib::BusinessDayConvention)fixedConvention ;
        _CfixedDayCount = safe_cast<CDayCounter^> (fixedDayCount);
        _CfixedDayCount->Lock();
        QuantLib::DayCounter& _fixedDayCount = static_cast<QuantLib::DayCounter&> (_CfixedDayCount->GetReference ()); 
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>::IsSome::get (spread))
        {
            _Cspread = safe_cast<CQuote^> (spread->Value);
            _Cspread->Lock();
        }
        Handle<QuantLib::Quote>& _spread = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>::IsSome::get (spread) ? static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()) : Handle<QuantLib::Quote>()); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (fwdStart))
        {
            _CfwdStart = safe_cast<CPeriod^> (fwdStart->Value);
            _CfwdStart->Lock();
        }
        QuantLib::Period& _fwdStart = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (fwdStart) ? static_cast<QuantLib::Period&> (_CfwdStart->GetReference ()) : 0*Days); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountingCurve))
        {
            _CdiscountingCurve = safe_cast<CYieldTermStructure^> (discountingCurve->Value);
            _CdiscountingCurve->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _discountingCurve = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountingCurve) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountingCurve->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppSwapRateHelper = new boost::shared_ptr<QuantLib::SwapRateHelper> (new QuantLib::SwapRateHelper ( _rate,  _tenor,  _calendar,  _fixedFrequency,  _fixedConvention,  _fixedDayCount,  _iborIndex,  _spread,  _fwdStart,  _discountingCurve ));
        SetRelativeDateRateHelper (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppSwapRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crate != nullptr) _Crate->Unlock();
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CfixedDayCount != nullptr) _CfixedDayCount->Unlock();
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
        if (_CfwdStart != nullptr) _CfwdStart->Unlock();
        if (_CdiscountingCurve != nullptr) _CdiscountingCurve->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (Cephei::QL::IQuote^ rate, Cephei::QL::Indexes::ISwapIndex^ swapIndex, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>^ spread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ fwdStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountingCurve) : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
    CQuote^ _Crate;
    CSwapIndex^ _CswapIndex;
    CQuote^ _Cspread;
    CPeriod^ _CfwdStart;
    CYieldTermStructure^ _CdiscountingCurve;
    try
    {
#ifdef HANDLE
        _phSwapRateHelper = NULL;
#endif
        _Crate = safe_cast<CQuote^> (rate);
        _Crate->Lock();
        Handle<QuantLib::Quote>& _rate = static_cast<Handle<QuantLib::Quote>&> (_Crate->GetHandle ()); 
        _CswapIndex = safe_cast<CSwapIndex^> (swapIndex);
        _CswapIndex->Lock();
        boost::shared_ptr<QuantLib::SwapIndex>& _swapIndex = static_cast<boost::shared_ptr<QuantLib::SwapIndex>&> (_CswapIndex->GetShared ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>::IsSome::get (spread))
        {
            _Cspread = safe_cast<CQuote^> (spread->Value);
            _Cspread->Lock();
        }
        Handle<QuantLib::Quote>& _spread = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>::IsSome::get (spread) ? static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()) : Handle<QuantLib::Quote>()); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (fwdStart))
        {
            _CfwdStart = safe_cast<CPeriod^> (fwdStart->Value);
            _CfwdStart->Lock();
        }
        QuantLib::Period& _fwdStart = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (fwdStart) ? static_cast<QuantLib::Period&> (_CfwdStart->GetReference ()) : 0*Days); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountingCurve))
        {
            _CdiscountingCurve = safe_cast<CYieldTermStructure^> (discountingCurve->Value);
            _CdiscountingCurve->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _discountingCurve = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountingCurve) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountingCurve->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppSwapRateHelper = new boost::shared_ptr<QuantLib::SwapRateHelper> (new QuantLib::SwapRateHelper ( _rate,  _swapIndex,  _spread,  _fwdStart,  _discountingCurve ));
        SetRelativeDateRateHelper (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppSwapRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crate != nullptr) _Crate->Unlock();
        if (_CswapIndex != nullptr) _CswapIndex->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
        if (_CfwdStart != nullptr) _CfwdStart->Unlock();
        if (_CdiscountingCurve != nullptr) _CdiscountingCurve->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (boost::shared_ptr<QuantLib::SwapRateHelper>& childNative, Object^ owner) : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
#ifdef HANDLE
	_phSwapRateHelper = NULL;
#endif
	_ppSwapRateHelper = &childNative;
    _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppSwapRateHelper));
}
Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (QuantLib::SwapRateHelper& childNative, Object^ owner) : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
#ifdef HANDLE
	_phSwapRateHelper = NULL;
#endif
	_ppSwapRateHelper = new boost::shared_ptr<QuantLib::SwapRateHelper> (&childNative);
    _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppSwapRateHelper));
    _SwapRateHelperOwner = owner;
    _RelativeDateRateHelperOwner = owner;
}

Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (CSwapRateHelper^ copy) : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
#ifdef HANDLE
	_phSwapRateHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwapRateHelper = new boost::shared_ptr<QuantLib::SwapRateHelper> (copy->GetShared());
        _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppSwapRateHelper));
    }
}
Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (System::Type^ t) : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
#ifdef HANDLE
	_phSwapRateHelper = NULL;
#endif
	if (!t->IsSubclassOf(CSwapRateHelper::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (QuantLib::Handle<QuantLib::SwapRateHelper>& childNative, Object^ owner)  : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
	_phSwapRateHelper = &childNative;
	_ppSwapRateHelper = &static_cast<boost::shared_ptr<QuantLib::SwapRateHelper>>(childNative.currentLink());
    _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppSwapRateHelper));
    _SwapRateHelperOwner = owner;
}
Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (QuantLib::Handle<QuantLib::SwapRateHelper> childNative)  : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
	_phSwapRateHelper = &childNative;
	_ppSwapRateHelper = &static_cast<boost::shared_ptr<QuantLib::SwapRateHelper>>(childNative.currentLink());
    _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppSwapRateHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CSwapRateHelper::CSwapRateHelper (QuantLib::SwapRateHelper childNative)  : CRelativeDateRateHelper(CSwapRateHelper::typeid)
{
#ifdef HANDLE
	_phSwapRateHelper = NULL;
#endif
	_ppSwapRateHelper = new boost::shared_ptr<QuantLib::SwapRateHelper> (new QuantLib::SwapRateHelper (childNative));
    _ppRelativeDateRateHelper = new boost::shared_ptr<QuantLib::RelativeDateRateHelper> (boost::dynamic_pointer_cast<QuantLib::RelativeDateRateHelper> (*_ppSwapRateHelper));
}
#endif

Cephei::QL::Termstructures::Yield::CSwapRateHelper::~CSwapRateHelper ()
{
    if (_ppSwapRateHelper != NULL)
    {
	    delete _ppSwapRateHelper;
        _ppSwapRateHelper = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CSwapRateHelper::!CSwapRateHelper ()
{
    if (_ppSwapRateHelper != NULL)
    {
	    delete _ppSwapRateHelper;
    }
}
QuantLib::SwapRateHelper& Cephei::QL::Termstructures::Yield::CSwapRateHelper::GetReference ()
{
    if (_ppSwapRateHelper == NULL) throw gcnew NativeNullException ();
	return **_ppSwapRateHelper;
}
boost::shared_ptr<QuantLib::SwapRateHelper>& Cephei::QL::Termstructures::Yield::CSwapRateHelper::GetShared ()
{
    if (_ppSwapRateHelper == NULL) throw gcnew NativeNullException ();
	return *_ppSwapRateHelper;
}
QuantLib::SwapRateHelper* Cephei::QL::Termstructures::Yield::CSwapRateHelper::GetPointer ()
{
    if (_ppSwapRateHelper == NULL) throw gcnew NativeNullException ();
	return &**_ppSwapRateHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwapRateHelper>& Cephei::QL::Termstructures::Yield::CSwapRateHelper::GetHandle ()
{
	if (_phSwapRateHelper == NULL)
	{
		_phSwapRateHelper = new Handle<QuantLib::SwapRateHelper> (*_ppSwapRateHelper);
	}
	return *_phSwapRateHelper;
}
#endif
bool Cephei::QL::Termstructures::Yield::CSwapRateHelper::HasNative () 
{
	return (_ppSwapRateHelper != NULL);
}

Cephei::QL::Times::IPeriod^ Cephei::QL::Termstructures::Yield::CSwapRateHelper::ForwardStart::get ()
{
    try
    {
    	QuantLib::Period& _rv = (QuantLib::Period&)(*_ppSwapRateHelper)->forwardStart ( );   
        Cephei::QL::Times::CPeriod^ _nrv = gcnew Cephei::QL::Times::CPeriod (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Yield::CSwapRateHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSwapRateHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Yield::ISwapRateHelper^ Cephei::QL::Termstructures::Yield::CSwapRateHelper::SetTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ t)
{
    CYieldTermStructure^ _Ct;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ct = safe_cast<CYieldTermStructure^> (t);
        _Ct->Lock();
        QuantLib::YieldTermStructure* _t = static_cast<QuantLib::YieldTermStructure*> (_Ct->GetPointer ()); 
    	(*_ppSwapRateHelper)->setTermStructure ( _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ct != nullptr) _Ct->Unlock();
    }
}
Double Cephei::QL::Termstructures::Yield::CSwapRateHelper::Spread::get ()
{
    try
    {
    	QuantLib::Spread _rv = (QuantLib::Spread)(*_ppSwapRateHelper)->spread ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IVanillaSwap^ Cephei::QL::Termstructures::Yield::CSwapRateHelper::Swap::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::VanillaSwap> _rv = (boost::shared_ptr<QuantLib::VanillaSwap>)(*_ppSwapRateHelper)->swap ( );   
        Cephei::QL::Instruments::CVanillaSwap^ _nrv = gcnew Cephei::QL::Instruments::CVanillaSwap (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::ISwapRateHelper^ Cephei::QL::Termstructures::Yield::CSwapRateHelper_Factory::Create (Double rate, Cephei::QL::Indexes::ISwapIndex^ swapIndex, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>^ spread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ fwdStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountingCurve)
{
    return gcnew CSwapRateHelper ( rate,  swapIndex,  spread,  fwdStart,  discountingCurve);
}
Cephei::QL::Termstructures::Yield::ISwapRateHelper^ Cephei::QL::Termstructures::Yield::CSwapRateHelper_Factory::Create (Double rate, Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum fixedFrequency, QL::Times::BusinessDayConventionEnum fixedConvention, Cephei::QL::Times::IDayCounter^ fixedDayCount, Cephei::QL::Indexes::IIborIndex^ iborIndex, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>^ spread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ fwdStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountingCurve)
{
    return gcnew CSwapRateHelper ( rate,  tenor,  calendar,  fixedFrequency,  fixedConvention,  fixedDayCount,  iborIndex,  spread,  fwdStart,  discountingCurve);
}
Cephei::QL::Termstructures::Yield::ISwapRateHelper^ Cephei::QL::Termstructures::Yield::CSwapRateHelper_Factory::Create (Cephei::QL::IQuote^ rate, Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum fixedFrequency, QL::Times::BusinessDayConventionEnum fixedConvention, Cephei::QL::Times::IDayCounter^ fixedDayCount, Cephei::QL::Indexes::IIborIndex^ iborIndex, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>^ spread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ fwdStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountingCurve)
{
    return gcnew CSwapRateHelper ( rate,  tenor,  calendar,  fixedFrequency,  fixedConvention,  fixedDayCount,  iborIndex,  spread,  fwdStart,  discountingCurve);
}
Cephei::QL::Termstructures::Yield::ISwapRateHelper^ Cephei::QL::Termstructures::Yield::CSwapRateHelper_Factory::Create (Cephei::QL::IQuote^ rate, Cephei::QL::Indexes::ISwapIndex^ swapIndex, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::IQuote^>^ spread, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ fwdStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountingCurve)
{
    return gcnew CSwapRateHelper ( rate,  swapIndex,  spread,  fwdStart,  discountingCurve);
}
